package pl.gda.pg.mif.sftatom.molprohelper.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

//import atom.SSH;
//import atom.ServerInfo;


public class ConnectionDialog extends JFrame {
	private JPasswordField passOlimp;
	private JTextField loginOlimp;
	private JPasswordField passInner;
	private JTextField loginInner;
	private JComboBox hostComboBox;
	
	private JButton okButton;
	private JButton cancelButton;

	public ConnectionDialog() {
	//zmiana wygladu 
		 try {
			 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				 if ("Nimbus".equals(info.getName())) {
					 UIManager.setLookAndFeel(info.getClassName());
					 break;
				 }
			 }
		 } catch (Exception e) {
			 e.printStackTrace();
		 }

		setTitle("LOGIN");
		
		JPanel panel = new JPanel(new BorderLayout());
		int numberOfRows = 6;
		int numberOfCols = 2;
		panel.setLayout(new GridLayout(numberOfRows,numberOfCols));
		
	//Olimp
		//login	
			JLabel loginOlimpLabel = new JLabel(" Login(Olimp): ");
			panel.add(loginOlimpLabel);
			loginOlimp = new JTextField();
			panel.add(loginOlimp);
		//pass	
			JLabel passOlimpLabel = new JLabel(" Password(Olimp): ");
			panel.add(passOlimpLabel);
			passOlimp = new JPasswordField();
			panel.add(passOlimp);
	ConnectionDialogActionListener listener = new ConnectionDialogActionListener();
	//InnerServer
		//hosts
			JLabel hostInnerLabel = new JLabel(" Inner Hosts: ");
			panel.add(hostInnerLabel);
			String[] hostStrings = { "aqualung.mif.pg.gda.pl", "amber.localmif" };
			hostComboBox = new JComboBox(hostStrings);
			hostComboBox.setSelectedIndex(1);
			hostComboBox.addActionListener(listener);
			panel.add(hostComboBox);
		//login	
			JLabel loginInnerLabel = new JLabel(" Login(Inner): ");
			panel.add(loginInnerLabel);
			loginInner = new JTextField();
			panel.add(loginInner);
		//pass	
			JLabel passInnerLabel = new JLabel(" Password(Inner): ");
			panel.add(passInnerLabel);
			passInner = new JPasswordField();
			panel.add(passInner);
			
	//OK button
		okButton = new JButton("OK");
		okButton.addActionListener(listener);
		panel.add(okButton);
		
	//Cancel button	
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(listener);
		panel.add(cancelButton);
		
		
		getContentPane().add(panel);
		pack();
	}
	
	private class ConnectionDialogActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource() == cancelButton) {
				System.exit(0);
			}
			if(e.getSource() == okButton) {
				String loginOlimpText = loginOlimp.getText().trim();
				char[] passOlimpText_tmp = passOlimp.getPassword();
				String passOlimpText = new String(passOlimpText_tmp).trim();
				//ServerInfo olimpInfo = new ServerInfo("olimp.mif.pg.gda.pl",loginOlimpText,passOlimpText);
				
				String loginInnerText = loginInner.getText().trim();
				char[] passInnerText_tmp = passOlimp.getPassword();
				String passInnerText = new String(passInnerText_tmp).trim();
				//ServerInfo innerInfo = new ServerInfo("",loginInnerText,passInnerText,22);
				
			//tworzy polaczenie z tunelem
				
			//towrzy zwykle polaczenie	
				
				
				if (!((loginOlimpText.length() == 0) || (passOlimpText.length() == 0) || (passInnerText.length() == 0) || (passInnerText.length() == 0))) {
					setVisible(false);
				//	SSH olimp = new SSH(olimpInfo,innerInfo);
				//	SSH innerServer = new SSH(innerInfo);
					
				} else {
					JOptionPane.showMessageDialog(null, "Fill all Fields", "Empty Fields", JOptionPane.ERROR_MESSAGE);
				}
			}
			if(e.getSource() == hostComboBox){
				if(hostComboBox.getSelectedIndex() == 1);
					
			}

		}


	}
	
	public static void main(String[] args){
		ConnectionDialog conn = new ConnectionDialog();
		conn.setVisible(true);
		conn.setLocationRelativeTo(null); 
	}
}
